const SysAuth = appRequire('model/desktop/SysAuth');
const PageResultVo = appRequire('vo/PageResultVo.js');
class SysAuthSequelizeDao {
  constructor() {

  }
  static async create() {
    await SysAuth.sync();
  }
  static async saveOne(auth) {
    try {
      await SysAuth.create(auth);
    } catch (error) {
      throw new Error(error);
    }
  }
  static async updateOne(auth) {
    try {
      await SysAuth.update(auth,{where:{id:auth.id}});
    } catch (error) {
      throw new Error(error);
    } 
  }
  static async deleteById(id) {
    try {
      await SysAuth.destroy({where:{id}});
    } catch (error) {
      throw new Error(error);
    }
  }
  static async deleteMany(ids) {
    try {
      ids = ids.split(",");
      console.log(ids);
      await SysAuth.destroy({where:{id:ids}});
    } catch (error) {
      throw new Error(error);
    }
  }
  /**
   * 定制化查找字段
   * @param {*} id 
   */
  static async findOne(id) {
    try {
      let data = await sequelize.query(`
      select 
      c.id,c.path,c.pid,c.auth_name as authName,c.auth_type as authType,
      t.type_name as authTypeName,c.auth_url as authUrl,c.auth_icon as authIcon,c.sort 
      from sys_auth c left join sys_auth_type t on c.auth_type = t.id 
      where c.id=?
      `, {
        replacements: [id],
        model: SysAuth,
        plain:true, //plain为true 返回一个对象
        raw:true,
        type: QueryTypes.SELECT
      });
      return data;
    } catch (error) {
      throw new Error(error);
    } 
  }
  static async findById(id) {
    try {
      let data = await SysAuth.findOne({
        where:{
          id
        },
        raw:true,
        attributes: { exclude: [] },
      });
      return data;
    } catch (error) {
      throw new Error(error);
    } 
  }
  static async findAllUserAuths() {
    try {
      let data = await sequelize.query(`
      select sa.id,sa.pid,sa.auth_name as authName,sa.path,
      st.type_name as authTypeName,sa.auth_url as authUrl,
      sa.auth_icon as authIcon,sa.sort,sa.create_time as createTime 
      from sys_auth sa left join sys_auth_type st on sa.auth_type=st.id 
      where sa.path not like '/e89d3ee0-78c2-11eb-8f69-ed5f7083e2ce/dc371300-84c6-11eb-bd72-b1b485ae53eb%'
      order by pid,sa.sort desc,sa.create_time desc
      `, {
        // replacements: sqlParams,
        model: SysAuth,
        // plain:true, //plain为true 返回一个对象
        raw:true,
        type: QueryTypes.SELECT
      });
      return data;
    } catch (error) {
      throw new Error(error);
    } 
  }
  static async findAll() {
    try {
      let data = await sequelize.query(`
      select sa.id,sa.pid,sa.auth_name as authName,sa.path,
      st.type_name as authTypeName,sa.auth_url as authUrl,
      sa.auth_icon as authIcon,sa.sort,sa.create_time as createTime 
      from sys_auth sa left join sys_auth_type st on sa.auth_type=st.id 
      order by pid,sa.sort desc,sa.create_time desc
      `, {
        // replacements: sqlParams,
        model: SysAuth,
        // plain:true, //plain为true 返回一个对象
        raw:true,
        type: QueryTypes.SELECT
      });
      return data;
    } catch (error) {
      throw new Error(error);
    } 
  }
  static async findByPage(page) {
    try {
      let offset = (page.pageNum-1)*page.pageSize;
      let limit = page.pageSize;
      let where = {};
      //动态拼装sql的where条件
      if(page.searchParams&&page.searchParams.authName){
        where.auth_name = {
          [Op.like]: `%${page.searchParams.authName}%`,
        }
      }
      let result = await SysAuth.findAndCountAll({
        where,offset,limit,raw:true,attributes:{exclude:[]},order:[["create_time","DESC"]]
      })
      console.log(result)
       return PageResultVo.success('success',result.count,result.rows);
    } catch (error) {
      throw new Error(error);
    }
  } 
}
module.exports = SysAuthSequelizeDao;